package com.example.pushup;

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.content.SharedPreferences;
import android.view.KeyEvent;
import android.widget.Toast;


public class ChallengeActivity extends Activity{
	
	SensorManager mySensorManager;
	Sensor myLightSensor;
	TextView textPushupCount;
	TextView textAccuCount;
	TextView textLightSensor;
	
	private boolean initialized;
	private float data;
	private float lastData;
	private float initialValue;
	private int count=0;
	private int n_AccuCount ;
	
	
	@Override
	public void onPause() {
		super.onPause();
				
		count =0;
		SharedPreferences pref = getSharedPreferences("CHALLENGE", MODE_PRIVATE); //"파일"
		SharedPreferences.Editor editor = pref.edit();
		editor.clear();
		editor.commit();
		
		editor.putInt("ACCUCOUNT", n_AccuCount); //키값
		editor.commit();		
		
	}
	
	
	@Override
	public void onResume() {
		super.onResume();
		
		SharedPreferences pref = getSharedPreferences("CHALLENGE", MODE_PRIVATE);
		n_AccuCount = pref.getInt("ACCUCOUNT", count); //count자리에 0써도딤, 없을때 값
	}
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.challenge);
		
		textLightSensor = (TextView)findViewById(R.id.lightsensor);
		textPushupCount = (TextView)findViewById(R.id.pushupCount);
		textAccuCount 	= (TextView)findViewById(R.id.accuCount);		
		 
		mySensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);       
		myLightSensor = mySensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
		initialized=false;
		
		Button button= (Button) findViewById(R.id.resetButton);
		MyListenerClass buttonListener= new MyListenerClass();
		button.setOnClickListener(buttonListener);
		
		if (myLightSensor == null)      
			textLightSensor.setText("No Light Sensor!");
		else
			mySensorManager.registerListener(lightSensorEventListener, myLightSensor,SensorManager.SENSOR_DELAY_FASTEST);
		
	}
	
	////
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	

	class MyListenerClass implements OnClickListener{
		public void onClick(View v){
			
			n_AccuCount = 0;
			count = 0;
			
			textAccuCount.setText("Challenge: " + String.valueOf(n_AccuCount));
			textPushupCount.setText("Count: " + String.valueOf(count));						
			
		}
	}
	
	
	SensorEventListener lightSensorEventListener= new SensorEventListener()
	{

		@Override
		public void onAccuracyChanged(Sensor sensor, int accuracy) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onSensorChanged(SensorEvent event) {
			
			// TODO Auto-generated method stub
			
			if(event.sensor.getType()==Sensor.TYPE_LIGHT)
			{
				data= event.values[0];
				
				if (!initialized)
				{
					initialValue= data;
					lastData= data;
					count= 0;
										 
					textPushupCount.setText("Count: " + String.valueOf(count));
					textAccuCount.setText("Challenge: " + String.valueOf(n_AccuCount));
					initialized = true;
				} else
				{	
					if(lastData<initialValue*0.4 && data>initialValue*0.4)
					{
						count++;
						textPushupCount.setText("Count: " + String.valueOf(count));
						
						if(count >= n_AccuCount+1) 
						{
							n_AccuCount = count;
							textAccuCount.setText("Challenge: " + String.valueOf(n_AccuCount));
						} 						
					}
					
					lastData= data;
				}
			}
			
		}};
		@Override
	     public boolean onKeyDown(int keyCode, KeyEvent event) {
	      // TODO Auto-generated method stub
			switch (keyCode) {
			
			case KeyEvent.KEYCODE_BACK:
				Toast.makeText(this, count + " 회 하셨습니다.", Toast.LENGTH_SHORT).show();
				onDestroy();
				break;
			}
			
			return super.onKeyDown(keyCode, event);
		}
}
